package cn.com.fuhuitong.main.home.ui.activity

import android.annotation.SuppressLint
import android.graphics.PixelFormat
import android.os.Bundle
import android.webkit.WebViewClient
import androidx.lifecycle.Observer
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.home.entity.CharityListEntity
import cn.com.fuhuitong.main.home.vm.CharityViewModel
import cn.com.fuhuitong.utils.WebViewUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.tencent.smtt.sdk.TbsVideo
import kotlinx.android.synthetic.main.activity_home_charity_details.*
import kotlinx.android.synthetic.main.item_case_list_video.view.*


@Route(path = ARouteConstance.ACTIVITY_URL_CHARITY_DETAILS)
class HomeCharityDetailsActivity : ViewModeActivity<CharityViewModel>() {

    private var fileVideoUrl: String? = null

    private var charityListEntity: CharityListEntity? = null

    override fun viewModel(): CharityViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_home_charity_details

    override fun initView() {

        image_charity_details_back.setOnClickListener {
            this.finish()
        }

        charityListEntity = intent?.getSerializableExtra(AppConstance.BUNDLE_CHARITY_ITEM)
                as? CharityListEntity
        charityListEntity?.let {
            this.fillData(
                image = it.image,
                name = it.title,
                address = it.address,
                startTime = it.start_time,
                endTime = it.end_time,
                isTimeCancel = it.is_time_cancel
            )
        }

        text_charity_details_sign.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_CHARITY_ENROLL)
                .withSerializable(AppConstance.BUNDLE_CHARITY_ITEM, charityListEntity)
                .navigation()
        }

        image_charity_details_play.setOnClickListener {
            if (fileVideoUrl == null)
                return@setOnClickListener
            if (!TbsVideo.canUseTbsPlayer(this))
                return@setOnClickListener
            window.setFormat(PixelFormat.TRANSLUCENT)
            TbsVideo.openVideo(this, fileVideoUrl, Bundle().apply {
                putInt("screenMode", 102)
            })
        }
    }

    override fun initViewMode() {
        viewModel.charityDetailsDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                this.fillData(
                    image = it.image,
                    name = it.title,
                    address = it.address,
                    startTime = it.start_time,
                    endTime = it.end_time,
                    content = it.content,
                    fileVideoUrl = it.file,
                    isTimeCancel = it.is_time_cancel
                )
            }
        })
    }

    override fun initData() {
        charityListEntity?.let {
            viewModel.charityDetails(it.id)
        }
    }

    @SuppressLint("SetTextI18n", "SetJavaScriptEnabled")
    private fun fillData(
        image: String? = null, name: String, address: String, startTime: Long, endTime: Long,
        content: String? = null, fileVideoUrl: String? = null,isTimeCancel:Int = 1
    ) {
        image?.let {
            Glide.with(image_charity_details_occupy).load(getImageUrl(it))
                .into(image_charity_details_occupy)
        }
        text_charity_details_name.text = name
        text_charity_details_address.text = "地址：$address"
        text_charity_details_time.text =
            "时间：${TimeUtils.millis2String(
                startTime * 1000, "yyyy-MM-dd HH:mm"
            )} - ${TimeUtils.millis2String(
                endTime * 1000, "yyyy-MM-dd HH:mm"
            )}"
        text_charity_details_sign.isEnabled = isTimeCancel == 1
        web_view_charity_details_content.webViewClient = WebViewClient()
        web_view_charity_details_content.settings.domStorageEnabled = true
        web_view_charity_details_content.settings.databaseEnabled = true
        web_view_charity_details_content.settings.javaScriptEnabled = true
        content?.let {
            WebViewUtils.loadFullScreenHtml(web_view_charity_details_content, it)
        }
        this.fileVideoUrl = "$BASE_HTTP_URL$fileVideoUrl"
    }

}